package com.bulletelc.apollopre.module.main.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.framework.BaseFragment;
import com.bulletelc.apollopre.module.cases.CaseListActivity;
import com.bulletelc.apollopre.module.display.product.details.ProductDetailsActivity;
import com.bulletelc.apollopre.module.display.product.resultList.ResultListActivity;
import com.bulletelc.apollopre.module.display.search.SearchActivity;
import com.bulletelc.apollopre.module.main.MainActivity;
import com.bulletelc.apollopre.module.richscan.RichScanActivity;
import com.bulletelc.apollopre.utils.MyUtil;
import com.zhy.autolayout.AutoLinearLayout;

/**
 * 作者：jerry
 * 日期：2018/7/6
 * 说明：首页3.0界面
 */

public class ZDTHomeNewFragment extends BaseFragment implements View.OnClickListener {

    private WebView webView;
    private AutoLinearLayout llRichScan, llClassify;
    private TextView tvSearch;

    @Override
    public int getLayoutRes() {
        return R.layout.zdt_home_new_fragment;
    }

    @Override
    public void assignViews() {
        webView = findView(R.id.webView);
        llRichScan = v.findViewById(R.id.llRichScan);
        llClassify =  v.findViewById(R.id.llClassify);
        tvSearch= v.findViewById(R.id.tvSearch);
    }

    @Override
    public void setListener() {
        llRichScan.setOnClickListener(this);
        llClassify.setOnClickListener(this);
        tvSearch.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initWebView();
        webView.loadUrl(NetWorkPath.HOST_PATH);
    }

    /**
     * 初始化WebView
     */
    private void initWebView() {
        WebSettings settings = webView.getSettings();

        //支持插件
        settings.setJavaScriptEnabled(true);//支持js
        settings.setDomStorageEnabled(true);

        //缩放操作
        settings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为False，则该WebView不可缩放
        settings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        settings.setSupportZoom(true); //支持缩放，默认为True。是下面那个的前提。

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不缓存
        settings.setDomStorageEnabled(true);
        settings.setDefaultTextEncodingName("utf-8");//设置编码格式
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        //设置自适应屏幕，两者合用
        settings.setUseWideViewPort(true); //将图片调整到适合WebView的大小
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //Todo
                if (url.contains("show/product/detail?id=")) {
                    productDetail(MyUtil.getUrlValueByName(url, "id"));
                } else if (url.contains("show/product/list?cids=")) {
                    productList(MyUtil.getUrlValueByName(url, "cids"));
                } else if (url.contains("design")) {
                    design();
                }
                return true;
            }
        });

    }

    /**
     * 商品详情
     * @param id
     */
    private void productDetail(String id) {
        Intent intent = new Intent(baseActivity,ProductDetailsActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_PRODUCT_ID, id);
        startActivity(intent);
    }

    /**
     * 分类商品
     * @param ids
     */
    private void productList(String ids) {
        Intent intent = new Intent(baseActivity, ResultListActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_CLASSIFY_ID, ids);
        baseActivity.startActivity(intent);
    }

    /**
     * 定制
     */
    private void design(){
        startActivity(new Intent(baseActivity, CaseListActivity.class));
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llRichScan:
                startActivity(new Intent(baseActivity,RichScanActivity.class));
                break;

            case R.id.tvSearch:
                startActivity(new Intent(baseActivity, SearchActivity.class));
                break;
            case R.id.llClassify:
                ((MainActivity) baseActivity).showClassify();
                break;
        }
    }
}
